package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.enums.GeneralStatusEnum;
import com.alibaba.citrus.cr.mn.order.center.common.constants.ForecastConstants;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.crforecastsales.domian.forecastorderline.model.ForecastOrderLine;
import com.epoch.app.crforecastsales.sdo.ForecastOrderLineSDO;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author yuanDeng
 * @Date 2021/9/17 14:43
 **/
public class ForecastOrderRepositoryConverter {
    public static Log log = Log.getLogger(ForecastOrderRepositoryConverter.class);

    public static ForecastOrderLineSDO toForecastOrderLineSDO(ForecastOrderLine source) {
        if (source == null) {
            return null;
        }
        ForecastOrderLineSDO target = new ForecastOrderLineSDO();
        target.setId(source.getId());
        target.setMainOrderId(source.getMainOrderId());
        target.setMainOrderBizId(source.getMainOrderBizId());
        target.setSerialNumber(source.getSerialNumber());
        target.setSourceLineNum(source.getSourceLineNum());
        target.setOuterLineId(source.getOuterLineId());
        target.setScItemId(source.getScItemId());
        target.setScItemTitle(source.getScItemTitle());
        target.setScItemOutCode(source.getScItemOutCode());
        target.setCustomerCode(source.getCustomerCode());
        target.setCustomerName(source.getCreatorName());
        target.setBizCode(source.getBizCode());
        target.setStatus(source.getStatus());
        target.setDealStatus(source.getDealStatus());
        target.setCollectStatus(source.getCollectStatus());
        target.setAnalysisReason(source.getAnalysisReason());
        target.setAnalysisTime(source.getAnalysisTime());
        target.setAllowCollectAmount(source.getAllowCollectAmount());
        target.setIdemId(source.getIdemId());
        target.setInventoryUnit(source.getInventoryUnit());
        target.setInventoryUnitCode(source.getInventoryUnitCode());
        target.setSaleUnit(source.getSaleUnit());
        target.setSaleUnitCode(source.getSaleUnitcode());
        target.setUnitConvert(source.getUnitConvert());
        target.setActivityCode(source.getActivityCode());
        target.setActivityName(source.getActivityName());
        target.setActivityPrice(source.getActivityPrice());
        target.setQuantity(source.getQuantity());
        target.setUnitPrice(source.getUnitPrice());
        target.setTotalFee(source.getTotalFee());
        target.setTotalActivityFee(source.getTotalActivityFee());
        target.setPredictReplyAmount(source.getPredictReplyAmount());
        target.setActualTotalReplyAmount(source.getActualTotalReplyAmount());
        target.setShouldPayFee(source.getShouldPayFee());
        target.setDiscountFee(source.getDiscountFee());
        target.setRefuseReason(source.getRefuseReason());
        target.setRefuseReasonCode(source.getRefuseReasonCode());
        target.setReplyStatus(source.getReplyStatus());
        target.setReplyTime(source.getReplyTime());
        target.setConfirmTime(source.getConfirmTime());
        target.setConfirmStatus(source.getConfirmStatus());
        target.setFinishedTime(source.getFinishedTime());
        target.setWarehouseArea(source.getWarehouseArea());
        target.setWarehouseLocation(source.getWarehouseLocation());
        target.setGoodsProductationTime(source.getGoodsProductationTime());
        target.setGmtCreate(source.getGmt_create());
        target.setGmtModified(source.getGmt_modified());
        target.setCreatorId(source.getCreatorId());
        target.setCreatorName(source.getCreatorName());
        target.setModifierId(source.getModifierId());
        target.setCategoryChildDesc(source.getCategoryChildDesc());
        target.setCategoryChildCode(source.getCategoryChildCode());
        target.setCategoryCode(source.getCategoryCode());
        target.setCategoryDesc(source.getCategoryDesc());
        try {
            if (!StringUtils.isEmpty(source.getFeatures())) {
                JSONObject features = JSONObject.parseObject(source.getFeatures());
                target.setFeatures(features);
                if (features.containsKey(ForecastConstants.ACTIVITY_PRICE_REMARK)) {
                    target.setActivityPriceRemark(features.getString(ForecastConstants.ACTIVITY_PRICE_REMARK));
                }
                if (features.containsKey(ForecastConstants.ACTIVITY_NUMBER)) {
                    target.setActivityNumber(features.getString(ForecastConstants.ACTIVITY_NUMBER));
                }
            } else {
                Map<String, Object> features = new HashMap<>();
                target.setFeatures(features);
            }
        } catch (Exception e) {
            log.error("解析features异常", e);
        }
        target.setDetailRemark(source.getDetailRemark());
        target.setSyncVersion(source.getSyncVersion());
        target.setTenantId(source.getTenantId());
        target.setExt(source.getExt());
        target.setExt1(source.getExt1());
        target.setExt2(source.getExt2());
        target.setExt3(source.getExt3());
        target.setExt4(source.getExt4());
        target.setExt5(source.getExt5());
        target.setShippingWarehouseCode(source.getShipWarehouseCode());
        target.setShippingWarehouseId(source.getShippingWarehouseId());
        target.setShippingWarehouseName(source.getShippingWarehouseName());
        target.setDeliveryFactoryCode(source.getFactoryCode());
        target.setStockCode(source.getStockLocation());
        target.setBizFeatures(source.getBizFeatures());
        return target;
    }

    public static ForecastOrderLine toForecastOrderLine(ForecastOrderLineSDO source) {
        if (source == null) {
            return null;
        }
        ForecastOrderLine target = new ForecastOrderLine();
        target.setGoodsProductationTime(source.getGoodsProductationTime());
        target.setMainOrderBizId(source.getMainOrderBizId());
        target.setModifierId(source.getModifierId());
        target.setModifierCode(source.getModofierCode());
        target.setModifierName(source.getModifierName());
        target.setId(source.getId());
        target.setDealStatus(source.getDealStatus());
//        target.setGmtCreate(source.getGmtCreate());
//        target.setGmtModified(source.getGmtModified());
        target.setGmt_create(source.getGmtCreate());
        target.setGmt_modified(source.getGmtModified());
        target.setCreatorId(source.getCreatorId());
        target.setModifierId(source.getModifierId());
        target.setBizCode(source.getBizCode());
        target.setOuterLineId(source.getOuterLineId());
        target.setMainOrderId(source.getMainOrderId());
        target.setStatus(source.getStatus());
        target.setCollectStatus(source.getCollectStatus());
        target.setAllowCollectAmount(source.getAllowCollectAmount());
        target.setCreatorName(source.getCreatorName());
        target.setAnalysisReason(source.getAnalysisReason());
        target.setAnalysisTime(source.getAnalysisTime());
        target.setIdemId(source.getIdemId());
        target.setActivityName(source.getActivityName());
        target.setActivityCode(source.getActivityCode());
        target.setCustomerCode(source.getCustomerCode());
        target.setSerialNumber(source.getSerialNumber());
        target.setDepartmentCode(source.getDepartmentCode());
        target.setScItemId(source.getScItemId());
        target.setScItemTitle(source.getScItemTitle());
        target.setScItemOutCode(source.getScItemOutCode());
        target.setPredictReplyAmount(source.getPredictReplyAmount());
        target.setQuantity(source.getQuantity());
        target.setUnitPrice(source.getUnitPrice());
        target.setTotalFee(source.getTotalFee());
        target.setActivityPrice(source.getActivityPrice());
        target.setTotalActivityFee(source.getTotalActivityFee());
        target.setActualTotalReplyAmount(source.getActualTotalReplyAmount());
        target.setShouldPayFee(source.getShouldPayFee());
        target.setDiscountFee(source.getDiscountFee());
        target.setDetailRemark(source.getDetailRemark());
        target.setReplyStatus(source.getReplyStatus());
        target.setReplyTime(source.getReplyTime());
        target.setRefuseReasonCode(source.getRefuseReasonCode());
        target.setRefuseReason(source.getRefuseReason());
        target.setConfirmTime(source.getConfirmTime());
        target.setConfirmStatus(source.getConfirmStatus());
        target.setFinishedTime(source.getFinishedTime());
        target.setInventoryUnit(source.getInventoryUnit());
        target.setSaleUnit(source.getSaleUnit());
        target.setSaleUnitcode(source.getSaleUnitCode());
        target.setWarehouseArea(source.getWarehouseArea());
        target.setUnitConvert(source.getUnitConvert());
        target.setWarehouseLocation(source.getWarehouseLocation());
        target.setShipWarehouseCode(source.getShippingWarehouseCode());
        target.setShippingWarehouseName(source.getShippingWarehouseName());
        target.setShippingWarehouseId(source.getShippingWarehouseId());
        target.setFactoryCode(source.getDeliveryFactoryCode());
        target.setStockLocation(source.getStockCode());
        target.setFeatures(JSONObject.toJSONString(source.getFeatures()));
        target.setBizFeatures(source.getBizFeatures());
        target.setSyncVersion(source.getSyncVersion());
        target.setTenantId(source.getTenantId());
        target.setExt(source.getExt());
        target.setExt1(source.getExt1());
        target.setExt2(source.getExt2());
        target.setExt3(source.getExt3());
        target.setExt4(source.getExt4());
        target.setExt5(source.getExt5());
        if (Objects.nonNull(source.getCloseFlag())) {
            target.setClosed(source.getCloseFlag() ? GeneralStatusEnum.YES.getCode() : GeneralStatusEnum.NO.getCode());
        }
        return target;
    }
}
